<template>
  <div>
    <el-card class="box-card margin_30">
      <div slot="header" class="clearfix">
        <el-button type="primary" size="small" @click="dialogFormVisible = true">新增</el-button>
      </div>
      <div>
        <el-table
          :data="addressList"
          border
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="#"
            fixed="left"
            width="50"
            align="center"
          />
          <el-table-column
            label="地址名称"
            align="center"
            width="250"
            prop="addressName"
          />
          <el-table-column
            label="默认发货地址"
            align="center"
            width="200"
            prop="sendStatus"
          >

            <template slot-scope="scope">
              默认发货地址:
              <el-switch
                v-model="scope.row.sendStatus"
                active-color="#409eff"
                @change="upSendStatus(scope.row.id)"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="收货人姓名"
            width="300"
            align="center"
            prop="name"
          />
          <el-table-column
            label="收货人电话"
            align="center"
            width="200"
            prop="phone"
          />
          <el-table-column
            label="收货人地址"
            align="center"
            width="200"
            prop="receiveAddress"
          />
          <el-table-column
            label="默认收货地址"
            align="center"
            width="200"
            prop="receiveStatus"
          >

            <template slot-scope="scope">
              默认收货地址:
              <el-switch
                v-model="scope.row.receiveStatus"
                active-color="#409eff"
                @change="upReceiveStatus(scope.row.id)"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="创建事件"
            align="center"
            width="200"
            prop="createTime"
          />
          <el-table-column
            label="操作"
            fixed="right"
            align="center"
            width="150"
          >
            <template slot-scope="scope">
              <el-link type="primary" @click="upAddress(scope.row.id)">编辑</el-link>
              <el-link type="danger" @click="delAddress(scope.row.id)">删除</el-link>
            </template>

          </el-table-column>
        </el-table>
      </div>
    </el-card>

    <el-dialog title="地址详情" :visible.sync="dialogFormVisible">
      <el-form :model="addressform" size="mini">
        <el-form-item label="地址名称" :label-width="formLabelWidth">
          <el-input v-model="addressform.addressName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="收货人姓名" :label-width="formLabelWidth">
          <el-input v-model="addressform.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="收货人手机号" :label-width="formLabelWidth">
          <el-input v-model="addressform.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮政编码" :label-width="formLabelWidth">
          <el-input v-model="addressform.postCode " autocomplete="off" />
        </el-form-item>
        <el-form-item label="收货地址" :label-width="formLabelWidth">
          <el-cascader
            v-model="selectedOptions"
            size="large"
            :options="options"
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="详细地址" :label-width="formLabelWidth">
          <el-input v-model="addressform.detailAddress " autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { regionData, CodeToText } from 'element-china-area-data'
import { addressList, setSendOne, companyAddress, update, del,
  setReceiveOne, save } from '@/api/orderManage/addressManage/index'
export default {
  name: 'VueAdminTemplateMasterIndex',

  data() {
    return {
      selectedOptions: [],
      addressList: [],
      dialogFormVisible: false,
      addressform: {
        'addressName': '',
        'city': '',
        'cityCode': '',
        'createTime': '',
        'detailAddress': '',
        'id': '',
        'modifyTime': '',
        'name': '',
        'phone': '',
        'postCode': '',
        'province': '',
        'receiveStatus': 0,
        'region': '',
        'sendStatus': 0
      },
      formLabelWidth: '120px',
      options: regionData
    }
  },

  created() {
    this.init()
  },

  mounted() {

  },

  methods: {
    delAddress(id) {
      del(id).then(res => {
        this.init()
      })
    },
    upAddress(id) {
      this.addressform.id = id
      companyAddress(id).then(res => {
        this.selectedOptions = this.addressform.cityCode.split(',')
        this.addressform = res.data.address
        this.dialogFormVisible = true
        console.log(res)
      })
    },
    saveAdd() {
      if (this.addressform.id) {
        update(this.addressform).then(res => {
          console.log(res)
          this.dialogFormVisible = false
          this.init()
        })
      } else {
        save(this.addressform).then(res => {
          console.log(res)
          this.dialogFormVisible = false
          this.init()
        })
      }
    },
    handleChange(val) {
      this.addressform.province = CodeToText[val[0]]
      this.addressform.city = CodeToText[val[1]]
      this.addressform.region = CodeToText[val[2]]
      console.log(val)
      this.addressform.cityCode = val.join(',')
    },
    init() {
      addressList().then(res => {
        res.data.items.forEach(ele => {
          ele.sendStatus = !!ele.sendStatus
          ele.receiveStatus = !!ele.receiveStatus
          ele.receiveAddress = ele.province + ele.city + ele.region + ele.detailAddress
        })

        this.addressList = res.data.items
        console.log(this.addressList)
      })
    },
    upSendStatus(id) {
      setSendOne({
        id
      }).then(res => {
        this.init()
      })
    },
    upReceiveStatus(id) {
      setReceiveOne({
        id
      }).then(res => {
        this.init()
      })
    }
  }
}
</script>

<style scoped>

</style>
